package enemyObject;

import com.sxt.*;
import OwnSideObject.BulletObj;
import com.sxt.BacGroundObj;
import com.sxt.GameObject;
import com.sxt.GameWin;

import java.awt.*;

public class Enemy2Obj extends EnemyObject{
    public static int ENEMY_2_WIDTH = 69; //二号敌机的宽度
    public static int ENEMY_2_HEIGHT = 99; //二号敌机的高度
    //敌方2号飞机爆炸效果动画图片张数
    public static int AnimationNums = 4;
    //敌方2号飞机爆炸动画效果图片路径及格式
    public static String AnimationPath = "images/enemy2_down";
    public static String PicFormat = ".png";
    //2号战机移动速度
    int speed = 7;
    //2号战机的血量
    public int initialLife = 3;
    int life = initialLife;
    public Enemy2Obj() {
        super();
    }

    public Enemy2Obj(String image, GameWin frame) {
        super(image, frame);
    }

    public Enemy2Obj(String image, int x, int y, int speedY, GameWin frame,int flag) {
        super(image, x, y, speedY, frame,flag);
        this.score=5;
    }

    public Enemy2Obj(String image, int x, int y, int width, int height, int speedY, GameWin frame,int flag) {
        super(image, x, y, width, height, speedY, frame,flag);
    }

    @Override
    public void paintSelf(Graphics s) {
        //动画替身
        if (this.flag==2){
            this.Frame.animationObjs.add(new AnimationObj(AnimationPath, PicFormat,AnimationNums,x,y,this.Frame));
            this.Frame.removeList.add(this);
        }

        s.drawImage(image,x,y,null);
        y+=speed;
        //越界敌机删除
        if (y>GameWin.WINDOW_HEIGHT){
            y=GameWin.ENEMY_VANISH_LOCATION_Y;
            x=GameWin.ENEMY_VANISH_LOCATION_X;
            this.Frame.removeList.add(this);
        }
        //画出敌机血条
        s.setColor(Color.RED);
        s.drawRect(x, y - 10, ENEMY_2_WIDTH, 5);
        s.fillRect(x, y - 10, (int)(ENEMY_2_WIDTH * ((double)life / initialLife)), 5);
        //敌方二号飞机发射子弹
        if (GameWin.count %50==0){
            FlipObj Enemy2LaunchBullet = new FlipObj("images/enemy2_hit.png",this.x,this.y,0,this.Frame);
            this.Frame.gameObjectList.add(Enemy2LaunchBullet);
            this.Frame.enemyBulletObjs.add(new EnemyBulletObj("images/bullet1.png",this.x + 35,this.y + 100, 10, this.Frame,1));
            this.Frame.gameObjectList.add(this.Frame.enemyBulletObjs.get(this.Frame.enemyBulletObjs.size() - 1));
        }

        //与我方每一发子弹进行碰撞检测
        if (!this.Frame.bulletObjList.isEmpty())
            for (BulletObj bulletObj:this.Frame.bulletObjList){
                if (this.getRec().intersects(bulletObj.getRec())){
                    life--;
                    GameMusic.enemy2_down.play();
                    bulletObj.x=GameWin.MY_BULLET_VANISH_LOCATION_X;
                    bulletObj.y=GameWin.MY_BULLET_VANISH_LOCATION_Y;
                    if(life <= 0){
                        this.Frame.animationObjs.add(new AnimationObj(AnimationPath, PicFormat,AnimationNums,x,y,this.Frame));
                        this.Frame.removeList.add(bulletObj);
                        this.x=GameWin.MY_BULLET_VANISH_LOCATION_X;
                        this.y=GameWin.MY_BULLET_VANISH_LOCATION_Y;
                        this.Frame.removeList.add(this);
                        BacGroundObj.score += 3;
                    }

                }
            }
    }

    @Override
    public Rectangle getRec() {
        return new Rectangle(x,y,ENEMY_2_WIDTH,ENEMY_2_HEIGHT);
    }

    @Override
    public void DeBug() {
        System.out.println("Enemy2Obj Class");
    }
}
